local BaseSprite = import('..views.BaseSprite')

EventDispatchCentre = class('EventDispatchCentre')

--[[
gDestoryPool={sprite,score,notDisplayscore}
sprite:要删除的对象
score:得分
notDisplayscore:是否要显示得分
]]
gDestoryPool = {}

isDoingBasketEffect = false


function EventDispatchCentre:ctor()
	cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

	self:addEventListener("onTouchUp", onTouchUp)
	self:addEventListener("onTouchDown", onTouchDown)
	self:addEventListener("onTouchLeft", onTouchLeft)
	self:addEventListener("onTouchRight", onTouchRight) 
	self:addEventListener("onTouchSelected", onTouchSelected) 
	self:addEventListener("onTouchCancelSelected", onTouchCancelSelected) 

	self:addEventListener("cleanupDestoryPool", cleanupDestoryPool)
	self:addEventListener("doDestoryEffect", doDestroyByEffects) 
end


function onTouchUp(event)
	--print('... onTouchUp ...')
	local col,row = event.point.col,event.point.row
	local sprite = gSprites[row][col]
	--sprite:printBaseSpriteInfo()

	if sprite:isCanMove('up') then
		--gTouchEnable = false
		local sprite2 = gSprites[row+1][col]
		if isTowEffect(sprite,sprite2) then return end
		--先将sprite交换
		sprite.row = sprite.row + 1
		sprite2.row = sprite2.row - 1
		gSprites[row][col] = sprite2
		gSprites[row+1][col] = sprite
		
		if  judgeMoveResult(sprite,sprite2,'up') then
			--print('judgeMoveResult false in up')
			gSprites[row][col] = gSprites[row+1][col]
			gSprites[row+1][col] = sprite2
			gSprites[row][col].row = gSprites[row][col].row - 1
			gSprites[row+1][col].row = gSprites[row+1][col].row + 1
		end
	end
end

function onTouchDown(event)
	--print('... onTouchDown ...')
	
	local col,row = event.point.col,event.point.row
	local sprite = gSprites[row][col]
	--sprite:printBaseSpriteInfo()
	if not sprite then return end
	if sprite:isCanMove('down') then
		--gTouchEnable = false
		local sprite2 = gSprites[row-1][col]
		if isTowEffect(sprite,sprite2) then return end
		--先将sprite交换
		sprite.row = sprite.row - 1
		sprite2.row = sprite2.row + 1
		gSprites[row][col] = sprite2
		gSprites[row-1][col] = sprite
		if  judgeMoveResult(sprite,sprite2,'down') then
			--print('judgeMoveResult false in down')
			gSprites[row][col] = sprite
			gSprites[row-1][col] = sprite2
			sprite.row = sprite.row + 1
			sprite2.row = sprite2.row - 1
		end
	end
end

function onTouchLeft(event)
	--print('... onTouchLeft ...')
	
	local col,row = event.point.col,event.point.row
	local sprite = gSprites[row][col]
	--sprite:printBaseSpriteInfo()
	if not sprite then return end
	if sprite:isCanMove('left') then
		--gTouchEnable = false
		local sprite2 = gSprites[row][col-1]
		if isTowEffect(sprite,sprite2) then return end
		--先将sprite交换
		sprite.col = sprite.col - 1
		sprite2.col = sprite2.col + 1
		gSprites[row][col] = sprite2
		gSprites[row][col-1] = sprite
		if  judgeMoveResult(sprite,sprite2,'left') then
			--print('judgeMoveResult false in left')
			gSprites[row][col-1] = sprite2
			gSprites[row][col] = sprite
			sprite.col = sprite.col + 1
			sprite2.col = sprite2.col - 1
		end
	end
end

function onTouchRight(event)
	--print('... onTouchRight ...')
	
	local col,row = event.point.col,event.point.row
	local sprite = gSprites[row][col]
	local sprite2
	--sprite:printBaseSpriteInfo()
	if not sprite then return end
	if sprite:isCanMove('right') then
		--gTouchEnable = false
		local sprite2 = gSprites[row][col+1]
		if isTowEffect(sprite,sprite2) then return end
		--先将sprite交换
		sprite.col = sprite.col + 1
		sprite2.col = sprite2.col - 1
		gSprites[row][col] = sprite2
		gSprites[row][col+1] = sprite
		if 	judgeMoveResult(sprite,sprite2,'right') then
			--print('judgeMoveResult false in right')
			gSprites[row][col] = sprite
			gSprites[row][col+1] = sprite2
			sprite.col = sprite.col - 1
			sprite2.col = sprite2.col + 1
		end
	end
end



--[[
4,5,T消的时候不删除本身，同时转换为特效
]]
function setSpriteEffect(row,col,effectName)
	if gSprites[row][col].effect then
		
		doDestroyByEffect(col,row,gSprites[row][col].effect)
		doDestroyByEffect(col,row,effectName)
		return
	end
	gSprites[row][col].effect = effectName
	local scene = display.getRunningScene()
	local score
	if effectName == 'basket' then
		score = 180
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(11)
		local sprite = gSprites[row][col]
		local tag = sprite.childSpriteTag
		sprite:addChild(par)
		sprite:removeChildByTag(tag)
		print('... my sytle is 10... ')
		sprite.sytle = 10
		local child = display.newSprite('#basket.png'):addTo(sprite)
		child:setTag(tag)
		child:setScale(0.2)
		child:setLocalZOrder(2)
		local scale = cc.ScaleTo:create(0.6,1.0)
		child:runAction(scale)
	elseif effectName == 'bomb' then
		score = 180
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(11)
		gSprites[row][col]:addChild(par)
	elseif effectName == 'horizontal' then
		score = 120
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(12)
		gSprites[row][col]:addChild(par)
		local sp = display.newSprite('#effect.png')
		sp:setLocalZOrder(11)
		sp:setScale(0.8)
		sp:setOpacity(200)
		gSprites[row][col]:addChild(sp)
	else
		score = 120
		local par = cc.ParticleSystemQuad:create('bomb.plist')
		par:setLocalZOrder(12)
		gSprites[row][col]:addChild(par)
		local sp = display.newSprite('#effect.png')
		sp:setRotation(90)
		sp:setScale(0.8)
		sp:setLocalZOrder(11)
		sp:setOpacity(200)
		gSprites[row][col]:addChild(sp)
	end
---[[
	local x,y = BaseSprite.toPosition(col,row)
	local tag = gSprites[row][col].tag
	local text = cc.ui.UILabel.new({
            UILabelType = 2, text = score, size = 14})
        :align(display.CENTER, x, y)
        :addTo(scene)
      text:setTag(tag*100)
    local seq2 = cc.Sequence:create(
    	cc.MoveBy:create(1.2,cc.p(0,64)),
    	cc.CallFunc:create(function()
    		--printf('setSpriteEffect sequence callfunc,tag=%d',tag)
    		display.getRunningScene():removeChildByTag(tag*100)
    	end)
    )
    text:runAction(seq2)
    --]]
end



function doDestroyByEffects(event)
	doDestroyByEffect(event.effect[1],event.point.col,event.point.row)
	if event.effect[2] and event.effect[2] ~= event.effect[1] then
		doDestroyByEffect(event.effect[2],event.point.col,event.point.row)
	end
end

function doDestroyByEffect(effectname,col,row)
	--printf('do %s effect at (%d,%d)',effectname,col,row)
	gSprites[row] = gSprites[row] or {}
	if gSprites[row][col] then
		gSprites[row][col].effect = nil
		gSprites[row][col].conRemove = true
		gSprites[row][col].effect2 = nil
	end
	

	displayEfffect(col,row,effectname) 
	if effectname == 'basket' then
		--print('... do basket effect ...')
		--gTouchUnEnable_Effect_Backet = true  				--锁定触摸，不可用
		local sytles = {} 									--随机选定要消除的类型(边上1格内的)
		local hstart = (col - 1 < 1) and 1 or col - 1
		local hend = (col + 1 > 8 ) and 8 or col + 1
		local vstart = (row - 1 < 1) and 1 or row - 1
		local vend = (row + 1 > 9 ) and 9 or row + 1
		local sytle
		for r=vstart,vend do
			for c=hstart,hend do
				if baseMap[r][c] == 1 and baseWall[r][c] == 0 then
					gSprites[r] = gSprites[r] or {}
					local tmp = gSprites[r][c] 
					if tmp and tmp.sytle>0 and tmp.sytle<5 then
						local tmpSytle = tmp.sytle
						sytle = tmpSytle
						for _,v in ipairs(sytles) do
							if v == tmpSytle then
								sytle = nil
								break
							end
						end
						if sytle then sytles[#sytles+1] = sytle end
					end
				end
			end
		end
		if #sytles ~= 0 then
			sytle = sytles[math.random(1,#sytles)]
		else
			sytle = math.random(1,4)
		end
		doDestoryByBasketEffect(col,row,sytle)
	elseif effectname == 'bomb' then
		--print('... do bomb effect ...')
		local hstart = (col - 1 < 1) and 1 or col - 1
		local hend = (col + 1 > 8 ) and 8 or col + 1
		local vstart = (row - 1 < 1) and 1 or row - 1
		local vend = (row + 1 > 9 ) and 9 or row + 1
		--printf('hstart=%d,hend=%d',hstart,hend)
		--printf('vstart=%d,vend=%d',vstart,vend)
		for r=vstart,vend do
			for c=hstart,hend do
				if baseMap[r][c] == 1 and baseWall[r][c] == 0 then
					gSprites[r] = gSprites[r] or {}
					local tmp = gSprites[r][c] 
					if tmp then
						local destoryArgs = {}
						destoryArgs.score = 180
						destoryArgs.sprite = tmp 
						destoryArgs.notDisplayscore = false
						destoryArgs.changeToEffect = false
						gDestoryPool[#gDestoryPool+1] = destoryArgs
					end
				end
			end
		end
		--destroySprites(events)
	elseif effectname == 'horizontal' then
		--print('... do horizontal effect ...')
		for i=1,8 do
			if baseMap[row][i] == 1 and baseWall[row][i] == 0 then
				gSprites[row] = gSprites[row] or {}
				local tmp = gSprites[row][i]
				if tmp then
					if tmp.effect == effectname then tmp.effect = nil end 	--同一效果不在多触发
					local destoryArgs = {}
					destoryArgs.score = 120
					destoryArgs.sprite = tmp 
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs
				end
			end
		end
		--printf('... destorySprites number = %d',#events.sprites)
		--destroySprites(events)
	else
		--print('... do vertical effect ...')
		for i=1,9 do
			if baseMap[i][col] == 1 and baseWall[i][col] == 0 then
				gSprites[i] = gSprites[i] or {}
				local tmp = gSprites[i][col]
				if tmp then
					if tmp.effect == effectname then tmp.effect = nil end 	--同一效果不在多触发
					local destoryArgs = {}
					destoryArgs.score = 120
					destoryArgs.sprite = tmp 
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs
				end
			end
		end
		--destroySprites(events)
	end
	cleanupDestoryPool()
end

--[[
显示特效效果
]]
function displayEfffect(col,row,effectname)
	local scene = display.getRunningScene()
	if effectname == 'basket' then
		--print('... not set basket effect ...')
	elseif effectname == 'bomb' then
		if gStopSoundEffect then audio.playSound('bomb.mp3') end
		local hstart = (col - 1 < 1) and 1 or col - 1
		local hend = (col + 1 > 8 ) and 8 or col + 1
		local vstart = (row - 1 < 1) and 1 or row - 1
		local vend = (row + 1 > 9 ) and 9 or row + 1
		for r=vstart,vend do
			for c=hstart,hend do
				gSprites[r] = gSprites[r] or {}
				local tmp = gSprites[r][c] 
				if tmp then
					local x,y = BaseSprite.toPosition(c,r)
					local par = cc.ParticleSystemQuad:create('bomb.plist'):addTo(scene)
					par:setLocalZOrder(11)
					par:pos(x,y)
					par:setScale(0.2)
					par:runAction(
						cc.Sequence:create(
							cc.ScaleTo:create(0.4,1.2),
							cc.DelayTime:create(0.2),
							cc.RemoveSelf:create()
						)
					)
				end
			end
		end
	elseif effectname == 'horizontal' then
		if gStopSoundEffect then audio.playSound('horizontalEffect.mp3') end
		local x,y = BaseSprite.toPosition(col,row)
		local node = display.newNode():addTo(scene)
		node:pos(x,y)
		local sprite1 = display.newSprite('#lightEffect01.png'):addTo(node)
		sprite1:runAction(cc.ScaleTo:create(0.2,0.2))
		local sprite2 = display.newSprite('#lightEffect02.png'):addTo(node)
		sprite2:setOpacity(128)
		sprite2:runAction(cc.ScaleTo:create(0.2,1.2,1))
		local sprite3 = display.newSprite('#lightEffect03.png'):addTo(node)
		local x1 = 320 - x
		sprite3:pos(x1,0)
		sprite3:setScale(0.5,0.5)
		sprite3:setOpacity(128)
		sprite3:runAction(cc.MoveTo:create(0.2,cc.p(0,0)))
		local sprite4 = display.newSprite('#lightEffect03.png'):addTo(node)
		local x2 = 832 - x
		sprite4:pos(x2,0)
		sprite4:setRotation(180)
		sprite4:setOpacity(128)
		sprite4:setScale(0.5,0.5)
		sprite4:runAction(cc.MoveTo:create(0.2,cc.p(0,0)))
		local seq = cc.Sequence:create(
			cc.DelayTime:create(0.3),
			cc.RemoveSelf:create()
			)
		node:runAction(seq)
	else
		if gStopSoundEffect then audio.playSound('verticalEffect.mp3') end
		local x,y = BaseSprite.toPosition(col,row)
		local node = display.newNode():addTo(scene)
		--node:setScale(0.5)
		node:pos(x,y)
		--printf('node x,y = %d,%d',x,y)
		local sprite1 = display.newSprite('#lightEffect01.png'):addTo(node)
		sprite1:runAction(cc.ScaleTo:create(0.2,0.2))
		local sprite2 = display.newSprite('#lightEffect02.png'):addTo(node)
		sprite2:setOpacity(128)
		sprite2:setRotation(90)
		sprite2:runAction(cc.ScaleTo:create(0.2,1.2,1))
		local sprite3 = display.newSprite('#lightEffect03.png'):addTo(node)
		sprite3:setRotation(-90)
		local y1 = 32 - y
		sprite3:pos(0,y1)
		sprite3:setScale(0.5,0.5)
		sprite3:setOpacity(128)
		--printf('sprite3 x,y = %d,%d',x,y1)
		sprite3:runAction(cc.MoveTo:create(0.2,cc.p(0,0)))
		local sprite4 = display.newSprite('#lightEffect03.png'):addTo(node)
		local y2 = 608 - y
		sprite4:pos(0,y2)
		sprite4:setScale(0.5,0.5)
		--printf('sprite4 x,y = %d,%d',x,y2)
		sprite4:setRotation(90)
		sprite4:setOpacity(128)
		sprite4:runAction(cc.MoveTo:create(0.2,cc.p(0,0)))
		local seq = cc.Sequence:create(
			cc.DelayTime:create(0.3),
			cc.RemoveSelf:create()
			)
		node:runAction(seq)
	end
end

function doDestoryByBasketEffect(col,row,sytle)
	if gStopSoundEffect then audio.playSound('basketEffect.mp3') end
	isDoingBasketEffect = true
	local sprites = {}
	--筛选要删除的精灵
	for r=1,9 do
		for c=1,8 do
			if baseMap[r][c] == 1 and baseWall[r][c] == 0 then
				gSprites[r] = gSprites[r] or {}
				local tmpSprite = gSprites[r][c]
				if tmpSprite and tmpSprite.sytle == sytle and tmpSprite.conRemove==true then
					sprites[#sprites+1] = tmpSprite
					tmpSprite.conRemove = false
				end
			end
		end
	end

	local action = cc.RepeatForever:create(
		cc.Sequence:create(
			--cc.ScaleTo:create(0.1,1.0),,
			cc.ScaleBy:create(0.3,0.5),
			cc.ScaleBy:create(0.3,2)
			)
		)
	action:setTag(gSprites[row][col].destoryEffectActionTag)
	gSprites[row][col].effect = nil
	gSprites[row][col].conRemove = false
	gSprites[row][col]:runAction(action)

	local scene = display.getRunningScene()

	local x,y = BaseSprite.toPosition(col,row)
	local tag = gSprites[row][col].tag
	local text = cc.ui.UILabel.new({
            UILabelType = 2, text = 180, size = 14})
        :align(display.CENTER, x, y)
        :addTo(display.getRunningScene())
      text:setTag(tag*100)
    local seq2 = cc.Sequence:create(
    	cc.MoveBy:create(1.2,cc.p(0,64)),
    	cc.CallFunc:create(function()
    		--printf('setSpriteEffect sequence callfunc,tag=%d',tag)
    		display.getRunningScene():removeChildByTag(tag*100)
    	end)
    )
    text:runAction(seq2)
    scene.score = scene.score + 180

	for _,v in ipairs(sprites) do
		local c,r=v.col,v.row
		if v.effect then
			--printf('... doDestoryByBasketEffect: v.effect=%s ...',v.effect)
			--v.conRemove = true
			doDestroyByEffect(v.effect,c,r)
		else
			--gSprites[r][c] = nil
			local seq = cc.Sequence:create(
				cc.MoveBy:create(0.1,cc.p(-6,0)),
				cc.MoveBy:create(0.2,cc.p(12,0)),
				cc.MoveBy:create(0.1,cc.p(-6,0)),
				cc.MoveBy:create(0.2,cc.p(12,0)),
				cc.MoveBy:create(0.1,cc.p(-6,0)),
				cc.MoveBy:create(0.2,cc.p(12,0)),
				--cc.MoveBy:create(0.1,cc.p(-6,0)),
				--cc.MoveBy:create(0.2,cc.p(12,0)),
				--cc.MoveBy:create(0.1,cc.p(-6,0)),
				--cc.MoveBy:create(0.2,cc.p(12,0)),
				setBrizzerAnimation(c,r,col,row),
				cc.CallFunc:create(function()
					gSprites[row][col]:runAction(
						cc.Sequence:create(
							cc.ScaleBy:create(0.1,0.1),
							cc.CallFunc:create(function()
								--gTouchUnEnable_Effect_Backet = false
								if gSprites[row][col].gLandNumber > 0 then gLandNumber = gLandNumber - gSprites[row][col].gLandNumber end
								if gSprites[row][col].intentToPoint then intentToPoint[gSprites[row][col].intentToPoint.row][gSprites[row][col].intentToPoint.col]=0 end
								--gEnableLand = gEnableLand + 1
								--gEventDispatchCentre:dispatchEvent({name='hadRemoveAPoint', point={col=col,row=row }} )
								gSprites[row][col] = nil
								isDoingBasketEffect = false 
								gHintTimer = 0
								if row>1 and baseWall[row-1][col] == 1 then gWallSprites[row-1][col]:destoryWall() end
								if row<9 and baseWall[row+1][col] == 1 then gWallSprites[row+1][col]:destoryWall() end
								if col>1 and baseWall[row][col-1] == 1 then gWallSprites[row][col-1]:destoryWall() end
								if col<8 and baseWall[row][col+1] == 1 then gWallSprites[row][col+1]:destoryWall() end
								--landSprites({col=col})
							end),
							cc.RemoveSelf:create()
						)
					)
					gSprites[r][c] = nil
					if v.gLandNumber > 0 then gLandNumber = gLandNumber - v.gLandNumber end
					if v.intentToPoint then intentToPoint[v.intentToPoint.row][v.intentToPoint.col]=0 end
					--landSprites({col=c})
					--printf('getNumberOfRunningActions,%d',v:getNumberOfRunningActions())
					--gEventDispatchCentre:dispatchEvent({name='hadRemoveAPoint', point={col=c,row=r }} )
				end),
				cc.RemoveSelf:create()
			)
			local dot = cc.ParticleSystemQuad:create('dot.plist'):addTo(v)
			v:setScale(0.35)
			seq:setTag(v.destoryEffectActionTag)
			v:runAction(seq)

			--增加得分显示
			local x,y = BaseSprite.toPosition(v.col,v.row)
			dot = cc.ParticleSystemQuad:create('dot.plist')
			dot:addTo(scene)
			dot:pos(x,y)
			dot:runAction(cc.Sequence:create(
				cc.BezierTo:create(0.5,{cc.p(x,y),cc.p(100+(x-100)/4,640),cc.p(64,display.cy+64+48*(5-v.sytle))}),
				cc.RemoveSelf:create()
				)
			)
			local text = cc.ui.UILabel.new({
		            UILabelType = 2, text = 180, size = 14})
		        :align(display.CENTER, x, y)
		        :addTo(display.getRunningScene())
		      text:setTag(v.tag*100)
		    local seq2 = cc.Sequence:create(
		    	cc.MoveBy:create(1.2,cc.p(0,64)),
		    	cc.CallFunc:create(function()
		    		--printf('doRemoveSelf sequence callfunc,tag=%d',self.tag)
		    		display.getRunningScene():removeChildByTag(v.tag*100)
		    		gTouchEnable = true
		    	end)
		    )
		    text:runAction(seq2)
			scene.score = scene.score + 180

			if v.sytle == 1 then
		    	scene.sytle1Number = scene.sytle1Number + 1
	    		scene.sytle1NumberLable:setString(scene.sytle1Number)
		    elseif v.sytle == 2 then
		    	scene.sytle2Number = scene.sytle2Number + 1
		    	scene.sytle2NumberLable:setString(scene.sytle2Number)
	    	elseif v.sytle == 3 then
	    		scene.sytle3Number = scene.sytle3Number + 1
	    		scene.sytle3NumberLable:setString(scene.sytle3Number)
			elseif v.sytle == 4 then
				scene.sytle4Number = scene.sytle4Number + 1
				scene.sytle4NumberLable:setString(scene.sytle4Number)
			elseif v.sytle == 5 then
				scene.sytle5Number = scene.sytle5Number + 1
				scene.sytle5NumberLable:setString(scene.sytle5Number)
			end
			if r>1 and baseWall[r-1][c] == 1 then gWallSprites[r-1][c]:destoryWall() end
			if r<9 and baseWall[r+1][c] == 1 then gWallSprites[r+1][c]:destoryWall() end
			if r>1 and baseWall[r][c-1] == 1 then gWallSprites[r][c-1]:destoryWall() end
			if r<8 and baseWall[r][c+1] == 1 then gWallSprites[r][c+1]:destoryWall() end

		end
	end

    scene.scoreLable:setString(scene.score)
    scene.loadpar:setParent(scene.score/scene.total*100)
    --text:runAction(seq2)

end

function setBrizzerAnimation(col,row,tagerCol,tagerRow)
	--if not col or row then return end
	local x,y = BaseSprite.toPosition(col,row)
	local tx,ty = BaseSprite.toPosition(tagerCol,tagerRow)
	local cx,cy = (tx+x)/2,ty*1.5
	local d = 0
	if y > ty then d = 10
	else d = -10
	end
	local bz = cc.BezierTo:create(0.8,{cc.p(x,y),cc.p(cx,cy+d),cc.p(tx,ty)})
	return bz
end


function cleanupDestoryPool()
	--print('... cleanupDestoryPool count=%d  ...',#gDestoryPool)
	for k,v in pairs(gDestoryPool) do
		if v.sprite then
			--v.sprite:printBaseSpriteInfo()
			v.sprite:doRemoveSelf(v.score,v.notDisplayscore,v.changeToEffect)
			--printf('cleanupDestoryPool row=%d,col=%d,changeToEffect=%s',v.sprite.row,v.sprite.col,v.changeToEffect)
			--gDestoryPool[k]=nil
		else
			--print('cleanupDestoryPool no have v.sprite')
		end
		gDestoryPool[k]=nil
	end
end

function isTowEffect(sprite1,sprite2)
	if sprite1.effect == 'basket' and sprite2.effect == 'basket' then
		--print('... do double basket effect: clean all ...')
		return true
	elseif sprite1.effect == 'basket' then
		doDestoryByBasketEffect(sprite1.col,sprite1.row,sprite2.sytle)
		return true
	elseif sprite2.effect == 'basket' then
		doDestoryByBasketEffect(sprite2.col,sprite2.row,sprite1.sytle)
		return true
	elseif sprite1.effect and sprite2.effect then
		local destoryArgs = {}
		destoryArgs.sprite = sprite1 
		destoryArgs.score = 180
		destoryArgs.notDisplayscore = false
		destoryArgs.changeToEffect = false
		gDestoryPool[#gDestoryPool+1] = destoryArgs
		destoryArgs = {}
		destoryArgs.sprite = sprite2 
		destoryArgs.score = 180
		destoryArgs.notDisplayscore = false
		destoryArgs.changeToEffect = false
		gDestoryPool[#gDestoryPool+1] = destoryArgs
		cleanupDestoryPool()
		return true
	else
		return false
	end
end

--[[
判断移动结果，削除或者回移，清除不包括

]]
function judgeMoveResult(point1,point2,dir)
	local h1,v1,hp1,vp1 = BaseSprite.judgeItem(point1)
	local h2,v2,hp2,vp2 = BaseSprite.judgeItem(point2)
	local needMoveBack = true
	local destoryArgs = {}
	--point1:printBaseSpriteInfo()
	--point2:printBaseSpriteInfo()
	--[[
	if h1 then
		printf('judgeMoveResult h1=%d,hp1=(%d,%d)',h1,hp1.began,hp1.ended)
	end
	if v1 then
		printf('judgeMoveResult v1=%d,vp1=(%d,%d)',v1,vp1.began,vp1.ended)
	end
	if h2 then
		printf('judgeMoveResult h2=%d,hp2=(%d,%d)',h2,hp2.began,hp2.ended)
	end
	if v2 then
		printf('judgeMoveResult v2=%d,vp2=(%d,%d)',v2,vp2.began,vp2.ended)
	end
	--]]
	--
	if h1>2 or v1>2 or h2>2 or v2>2 then
		--1
		if h1>2 and v1>2 then 									--T消
			for i=hp1.began,hp1.ended do
				if i ~= point1.col then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[point1.row][i]  
					destoryArgs.score = 240
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			for i=vp1.began,vp1.ended do
				if i ~= point1.row then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[i][point1.col]  
					destoryArgs.score = 240
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point1 
			destoryArgs.score = 240
			destoryArgs.notDisplayscore = false
			destoryArgs.changeToEffect = 'basket'
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		elseif h1>2 then  
			--print('... in horizontal ...')                                         
			for i=hp1.began,hp1.ended do
				--printf('add gDestoryPool row=%d,col=%d',point1.row,i)
				if i ~= point1.col then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[point1.row][i]  
					destoryArgs.score = 60*(h1-2)
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point1  
			destoryArgs.score = 60*(h1-2)
			destoryArgs.notDisplayscore = false
			local toEffect = nil
			if h1>4 then toEffect = 'bomb' 							   --5消
			elseif h1 == 4 then toEffect = 'vertical' 				   --4消
			end
			if point1.effect and point1.effect ~= toEffect then 									   --如果之前存在特效，则增加一特效，删除时将同时触发
				point1.effect2 = toEffect
			else
				destoryArgs.changeToEffect = toEffect
			end
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		elseif v1>2 then                                           
			for i=vp1.began,vp1.ended do
				--printf('add gDestoryPool row=%d,col=%d',i,point1.col)
				if i ~= point1.row then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[i][point1.col]  
					destoryArgs.score = 60*(v1-2)
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point1  
			destoryArgs.score = 60*(v1-2)
			destoryArgs.notDisplayscore = false
			local toEffect = nil
			if v1>4 then toEffect = 'bomb' 							   --5消
			elseif v1 == 4 then toEffect = 'horizontal' 				   --4消
			end
			if point1.effect and point1.effect ~= toEffect then 									   --如果之前存在特效，则增加一特效，删除时将同时触发
				point1.effect2 = toEffect
			else
				destoryArgs.changeToEffect = toEffect
			end
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		end

		--2
		if h2>2 and v2>2 then 									--T消
			for i=hp2.began,hp2.ended do
				if i ~= point2.col then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[point2.row][i]  
					destoryArgs.score = 240
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			for i=vp2.began,vp2.ended do
				if i ~= point2.row then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[i][point2.col]  
					destoryArgs.score = 240
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point2 
			destoryArgs.score = 240
			destoryArgs.notDisplayscore = true
			destoryArgs.changeToEffect = 'basket'
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		elseif h2>2 then                                           
			for i=hp2.began,hp2.ended do
				--printf('add gDestoryPool row=%d,col=%d',point2.row,i)
				if i ~= point2.col then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[point2.row][i]  
					destoryArgs.score = 60*(h2-2)
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
					--printf('add gDestoryPool row=%d,col=%d',point2.row,i)
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point2  
			destoryArgs.score = 60*(h2-2)
			destoryArgs.notDisplayscore = false
			local toEffect = nil
			if h2>4 then toEffect = 'bomb' 							   --5消
			elseif h2 == 4 then toEffect = 'vertical' 				   --4消
			end
			if point2.effect and point2.effect ~= toEffect then 									   --如果之前存在特效，则增加一特效，删除时将同时触发
				point2.effect2 = toEffect
			else
				destoryArgs.changeToEffect = toEffect
			end
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		elseif v2>2 then                                           
			for i=vp2.began,vp2.ended do
				--printf('add gDestoryPool row=%d,col=%d',i,point2.col)
				if i ~= point2.row then
					destoryArgs = {}
					destoryArgs.sprite = gSprites[i][point2.col]  
					destoryArgs.score = 60*(v2-2)
					destoryArgs.notDisplayscore = false
					destoryArgs.changeToEffect = false
					gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
				end
			end
			destoryArgs = {}
			destoryArgs.sprite = point2  
			destoryArgs.score = 60*(v2-2)
			destoryArgs.notDisplayscore = false
			local toEffect = nil
			if v2>4 then toEffect = 'bomb' 							   --5消
			elseif v2 == 4 then toEffect = 'horizontal' 				   --4消
			end
			if point2.effect and point2.effect ~= toEffect then 									   --如果之前存在特效，则增加一特效，删除时将同时触发
				point2.effect2 = toEffect
			else
				destoryArgs.changeToEffect = toEffect
			end
			gDestoryPool[#gDestoryPool+1] = destoryArgs 				--往清除池里加入对象
		end

		if gStopSoundEffect then audio.playSound(DESTORY_AUDIO[math.random(1,3)]) end
		needMoveBack = false
	else

		needMoveBack = true
	end

	--printf('needMoveBack = %s',tostring(needMoveBack))
	if dir == 'up' then
		gSprites[point1.row][point1.col]:doActionUp(needMoveBack)
		gSprites[point2.row][point2.col]:doActionDown(needMoveBack)
	elseif dir == 'down' then
		gSprites[point1.row][point1.col]:doActionDown(needMoveBack)
		gSprites[point2.row][point2.col]:doActionUp(needMoveBack)
	elseif dir == 'right' then
		gSprites[point1.row][point1.col]:doActionRight(needMoveBack)
		gSprites[point2.row][point2.col]:doActionLeft(needMoveBack)
	elseif dir == 'left' then
		gSprites[point1.row][point1.col]:doActionLeft(needMoveBack)
		gSprites[point2.row][point2.col]:doActionRight(needMoveBack)
	else
		assert(false,'judgeMoveResult parameter dir error!!!')
	end

	if needMoveBack then 
		if gStopSoundEffect then audio.playSound('arim.mp3') end
	end

	return needMoveBack
end


return EventDispatchCentre